package com.nowcoder.community.service;

import com.nowcoder.community.dao.MessageMapper;
import com.nowcoder.community.entity.Message;
import com.nowcoder.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * <p>Title: MessageService</p>
 * <p>author 12427</p>
 *
 * @version ver1.0
 * @date 2022/8/18 11:26
 */
@Service
public class MessageService {
    @Autowired
    private MessageMapper messageMapper ;
    @Autowired
    private SensitiveFilter sensitiveFilter ;

    public List<Message> findConversation(int userId, int offset , int limit){
        return messageMapper.selectConversations(userId,offset,limit) ;
    }

    public int findConversationCount(int userId) {
        return messageMapper.selectConversationCount(userId) ;
    }

    public List<Message> findLetters(int userId , String conversationId , int offset , int limit){
        return messageMapper.selectLetters(userId ,conversationId,offset,limit) ;
    }

    public int findLetterCount(int userId , String conversationId){
        return messageMapper.selectLetterCount(userId , conversationId) ;
    }

    public int findLetterUnreadCount(int userId , String conversation){
        return messageMapper.selectLetterUnreadCount(userId , conversation) ;
    }

    public int addMessage(Message message){
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));
        return messageMapper.insertMessage(message) ;
    }

    public int readMessage(List<Integer> ids){
        return messageMapper.updateStatus(ids , 1) ;
    }

    /**
     * 根据id查找短信
     * @param id
     * @return
     */
    public Message findMessageById(int id){
        return messageMapper.selectMessage(id) ;
    }


    /**
     * 发送者删除了该短信  更新为状态2
     * @param id
     * @return
     */
    public int deleteLetterBySender(int id){
        return messageMapper.updateStatus(Arrays.asList(new Integer[]{id})  , 2) ;
    }

    /**
     * 接收者删除了该短信   更新为状态3
     * @param id
     * @return
     */
    public int deleteLetterByRecipient(int id){
        return messageMapper.updateStatus(Arrays.asList(new Integer[]{id})  , 3) ;
    }

    /**
     * 双方都删除了该短信  更新为状态4
     * @param id
     * @return
     */
    public int deleteLetterBoth(int id){
        return messageMapper.updateStatus(Arrays.asList(new Integer[]{id})  , 4) ;
    }


    public Message findLatestNotice(int userId , String topic){
        return messageMapper.selectLatestNotice(userId , topic) ;
    }

    public int findNoticeCount(int userId , String topic) {
        return messageMapper.selectNoticeCount(userId , topic) ;
    }


    public int findNoticeUnreadCount(int userId , String topic ) {
        return  messageMapper.selectNoticeUnreadCount(userId , topic) ;
    }

    public List<Message> findNotice(int userId , String topic , int offset , int limit){
        return messageMapper.selectNotice(userId , topic , offset , limit) ;
    }




}
